//
//  LFRemarkViewController.m
//  LoveFruit
//
//  Created by libbbb on 16/9/27.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "LFRemarkViewController.h"
#import "LFTextView.h"

#define kTextBorderColor     RGBCOLOR(227,224,216)
#undef  RGBCOLOR
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]

@interface LFRemarkViewController ()<UITextViewDelegate>

@property (nonatomic, strong) LFTextView * textView;

@property (nonatomic, strong) UIButton * sendButton;

@property (nonatomic, strong) UIView * aView;

//回收键盘
@property (nonatomic, strong)UITextField *textField;

//字数的限制
@property (nonatomic, strong)UILabel *wordCountLabel;

@end

@implementation LFRemarkViewController

//-(void)viewWillAppear:(BOOL)animated {
//    [self.view removeFromSuperview];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"订单备注";
    [self setupUI];
}

- (void)setupUI {
    //6个button
    UIButton *oneButton = [UIButton buttonWithType:UIButtonTypeSystem];
    UIButton *twoButton = [UIButton buttonWithType:UIButtonTypeSystem];
    UIButton *threeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    UIButton *fourButton = [UIButton buttonWithType:UIButtonTypeSystem];
    UIButton *fiveButton = [UIButton buttonWithType:UIButtonTypeSystem];
    UIButton *sixButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self setButon:oneButton Title:@"注意保质期"];
    [self setButon:twoButton Title:@"送货前打电话"];
    [self setButon:threeButton Title:@"尽快送货"];
    [self setButon:fourButton Title:@"到了打电话"];
    [self setButon:fiveButton Title:@"中午前送到"];
    [self setButon:sixButton Title:@"水果要新鲜"];
    [self.view addSubview:oneButton];
    [self.view addSubview:twoButton];
    [self.view addSubview:threeButton];
    [self.view addSubview:fourButton];
    [self.view addSubview:fiveButton];
    [self.view addSubview:sixButton];
    //button点击事件
    [oneButton addTarget:self action:@selector(oneButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [twoButton addTarget:self action:@selector(twoButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [threeButton addTarget:self action:@selector(threeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [fourButton addTarget:self action:@selector(fourButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [fiveButton addTarget:self action:@selector(fiveButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [sixButton addTarget:self action:@selector(sixButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [oneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(70);
        make.left.equalTo(self.view).offset(20);
    }];
    [twoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(oneButton);
        make.left.equalTo(oneButton.mas_right).offset(15);
    }];
    [threeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(twoButton);
        make.left.equalTo(twoButton.mas_right).offset(15);
    }];
    [fourButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(oneButton.mas_bottom).offset(10);
        make.left.equalTo(self.view).offset(20);
    }];
    [fiveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(fourButton);
        make.left.equalTo(fourButton.mas_right).offset(15);
    }];
    [sixButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(fiveButton);
        make.left.equalTo(fiveButton.mas_right).offset(15);
    }];
    //字数限制label
    self.wordCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.textView.frame.origin.x - 20,  self.textView.frame.size.height - 20, [UIScreen mainScreen].bounds.size.width - 40, 20)];
    _wordCountLabel.font = [UIFont systemFontOfSize:14.f];
    _wordCountLabel.textColor = [UIColor lightGrayColor];
    self.wordCountLabel.text = @"0/300";
    self.wordCountLabel.backgroundColor = [UIColor whiteColor];
    self.wordCountLabel.textAlignment = NSTextAlignmentRight;
    
    [self.view addSubview:self.textView];
    [self.textView addSubview:self.wordCountLabel];
    [self.view addSubview: self.sendButton];
    
}

- (void)oneButtonClick {
    
    if (self.textView.text == nil) {
        self.textView.text = @"注意保质期";
    }else {
        self.textView.text = [NSString stringWithFormat:@"%@ 注意保质期",self.textView.text];
    }
    [self textViewDidChange:self.textView];
}
- (void)twoButtonClick {
    if (self.textView.text == nil) {
        self.textView.text = @"送货前打电话";
    }else {
        self.textView.text = [NSString stringWithFormat:@"%@ 送货前打电话",self.textView.text];
    }
    [self textViewDidChange:self.textView];
}
- (void)threeButtonClick {
    if (self.textView.text == nil) {
        self.textView.text = @"尽快送货";
    }else {
        self.textView.text = [NSString stringWithFormat:@"%@ 尽快送货",self.textView.text];
    }
    [self textViewDidChange:self.textView];
}
- (void)fourButtonClick {
    if (self.textView.text == nil) {
        self.textView.text = @"到了打电话";
    }else {
        self.textView.text = [NSString stringWithFormat:@"%@ 到了打电话",self.textView.text];
    }
    [self textViewDidChange:self.textView];
}
- (void)fiveButtonClick {
    if (self.textView.text == nil) {
        self.textView.text = @"中午前送到";
    }else {
        self.textView.text = [NSString stringWithFormat:@"%@ 中午前送到",self.textView.text];
    }
    [self textViewDidChange:self.textView];
}
- (void)sixButtonClick {
    if (self.textView.text == nil) {
        self.textView.text = @"水果要新鲜";
    }else {
        self.textView.text = [NSString stringWithFormat:@"%@ 水果要新鲜",self.textView.text];
    }
    [self textViewDidChange:self.textView];
}

#pragma mark - textView
-(LFTextView *)textView{
    
    if (!_textView) {
        _textView = [[LFTextView alloc]initWithFrame:CGRectMake(20, 180, self.view.frame.size.width - 40, 100)];
        _textView.backgroundColor = [UIColor whiteColor];
        _textView.delegate = self;
        _textView.font = [UIFont systemFontOfSize:14.f];
        _textView.textColor = [UIColor blackColor];
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.editable = YES;
        _textView.layer.cornerRadius = 4.0f;
        _textView.layer.borderColor = kTextBorderColor.CGColor;
        _textView.layer.borderWidth = 0.5;
        _textView.placeholderColor = RGBCOLOR(0x89, 0x89, 0x89);
        //        _textView.placeholder = @"给商家留言,可输入对商家的需求(可不填)";
        
        
    }
    
    return _textView;
}


#pragma mark textField的字数限制

//在这个地方计算输入的字数
- (void)textViewDidChange:(UITextView *)textView
{
    NSInteger wordCount = textView.text.length;
    self.wordCountLabel.text = [NSString stringWithFormat:@"%ld/300",(long)wordCount];
    [self wordLimit:textView];
}
#pragma mark 超过300字不能输入
-(BOOL)wordLimit:(UITextView *)text{
    if (text.text.length < 300) {
        NSLog(@"%ld",text.text.length);
        self.textView.editable = YES;
        
    }
    else{
        self.textView.editable = NO;
        
    }
    return nil;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_textView resignFirstResponder];
    [_textField resignFirstResponder];
}

#pragma mark - 初始化提交按钮
- (UIButton *)sendButton{
    
    if (!_sendButton) {
        _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _sendButton.layer.cornerRadius = 2.0f;
        _sendButton.frame = CGRectMake(20, 300, self.view.frame.size.width - 40, 40);
        _sendButton.backgroundColor = [UIColor orangeColor];
        [_sendButton setTitle:@"提交" forState:UIControlStateNormal];
        [_sendButton addTarget:self action:@selector(sendFeedBack) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _sendButton;
    
}

#pragma mark - 提交按钮监听事件
- (void)sendFeedBack {
    NSLog(@"提交成功");
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"LFRemakNotification" object:nil userInfo:@{@"textStr": self.textView.text}];
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)setButon:(UIButton *)button Title:(NSString *)title {
    
    // 初始化工作，设置文字，frame等等，略
    [button setTitle:title forState:UIControlStateNormal];
    //文字颜色
    [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    // 按钮边框宽度
    button.layer.borderWidth = 1.5;
    // 设置圆角
    button.layer.cornerRadius = 4.5;
    //文字位置
    button.contentEdgeInsets = UIEdgeInsetsMake(10,10, 10, 10);
    // 设置颜色空间为rgb，用于生成ColorRef
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    // 新建一个红色的ColorRef，用于设置边框（四个数字分别是 r, g, b, alpha）
    CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){ 0.2, 0, 0, 1 });
    // 设置边框颜色
    button.layer.borderColor = borderColorRef;
    
}

@end
